
%% Derivative Matrix;
TempFun     =   @(Param)SubFun_Sensitivity_Eval(PP,SS,MODEL,EquJac,Info,Param);
Mom         =   TempFun(ParamVec);
optset('parfdjac','tol',1e-2);
Jac_Mom2Param   =   parfdjac(TempFun,ParamVec);

%% Sensitivity
% Moment to Parameters
S_Mom2Param     =   Jac_Mom2Param./(abs(Mom(:))./abs(ParamVec(:)'));

% Parameters to Moments
WeightMat   =   diag(Info.Weight.^2);
% WeightMat       =   diag(ones(length(Info.Weight),1));
Jac_Param2Mom   =   -(Jac_Mom2Param'*WeightMat*Jac_Mom2Param)\(Jac_Mom2Param'*WeightMat);
S_Param2Mom     =   Jac_Param2Mom./(abs(ParamVec(:))./abs(Mom(:)'));

save('Sensitivity.mat','Jac_Mom2Param','Jac_Param2Mom','Mom');


%% Visualize the heat map

%--------------------------------------------------------------------------
% Setup
%--------------------------------------------------------------------------
List_Params     =   {'$\rho_{M}$','$\rho^{H}_{Y}$',...
                     '$\lambda_{\pi}$','$\lambda_{i}$','$\theta$'};
List_Moments    =   {'C to $\epsilon_{M}$','ir to $\varepsilon_{M}$',...
                     'CPI to $\varepsilon_{M}$',...
                     '$ \frac{Exchange\ Rate\ to\ \varepsilon^{H}_{Y}}{ C\ to\ \varepsilon^{H}_{Y}} $',...
                     '$ \frac{Export\ to\ \varepsilon^{H}_{Y}}{C\ to\ \varepsilon^{H}_{Y}} $'};
ColorMap_Param2Mom  = Graph_ColorMap(S_Param2Mom);
ColorMap_Mom2Param  = Graph_ColorMap(S_Mom2Param);


set(groot,'defaulttextinterpreter','Latex');  
set(groot, 'defaultAxesTickLabelInterpreter','Latex');  
set(groot, 'defaultLegendInterpreter','Latex'); 

fig_size    =   [1,1].*[1,0.25];
fig_gap     =   [0.02,0.02];
fig_VMargin =   [0.15,0.02];
fig_HMargin =   [0.15,0.02]; 

fig_SubPlot =   @(ii_vv)subtightplot(1,1,ii_vv,fig_gap,fig_VMargin,fig_HMargin);


% Figure

Fig         =   FigureSetup('Parameters to Moments',fig_size);
ax          =   fig_SubPlot(1);
ZAxis   =   S_Param2Mom; ZAxis = [ZAxis;ZAxis(end,:)]; ZAxis = [ZAxis,ZAxis(:,end)];
XAxis   =   (0:2:2*(size(ZAxis,2)-1));
YAxis   =   (0:2:2*(size(ZAxis,1)-1));
ax = surf(XAxis,YAxis,ZAxis);
xlim([min(XAxis),max(XAxis)]);
ylim([min(YAxis),max(YAxis)]);
xticks((1:2:max(XAxis)-1));yticks((1:2:max(YAxis)-1));
xticklabels(List_Moments);yticklabels(List_Params);
colormap(ColorMap_Param2Mom);colorbar;
for ii=1:size(S_Param2Mom,2)
    for jj=1:size(S_Param2Mom,1)
        text(2*(ii-1)+1-0.5,2*(jj-1)+1,S_Param2Mom(jj,ii)+500,num2str(round(S_Param2Mom(jj,ii),2),'%.2g'));
    end
end
view(2);

print('-depsc','-r1000',Fig,'TableGraphs/Identification/Sensitivity_Param2Mom');
% close(Fig);

Fig         =   FigureSetup('Parameters to Moments',fig_size);
ax          =   fig_SubPlot(1);
ZAxis   =   S_Mom2Param; ZAxis = [ZAxis;ZAxis(end,:)]; ZAxis = [ZAxis,ZAxis(:,end)];
XAxis   =   (0:2:2*(size(ZAxis,2)-1));
YAxis   =   (0:2:2*(size(ZAxis,1)-1));
ax = surf(XAxis,YAxis,ZAxis);
xlim([min(XAxis),max(XAxis)]);
ylim([min(YAxis),max(YAxis)]);
xticks((1:2:max(XAxis)-1));yticks((1:2:max(YAxis)-1));
yticklabels(List_Moments);xticklabels(List_Params);
colormap(ColorMap_Mom2Param);colorbar;
for ii=1:size(S_Mom2Param,2)
    for jj=1:size(S_Mom2Param,1)
        text(2*(ii-1)+1-0.5,2*(jj-1)+1,S_Mom2Param(jj,ii)+5,num2str(round(S_Mom2Param(jj,ii),2),'%.2g'));
    end
end
view(2);

print('-depsc','-r1000',Fig,'TableGraphs/Identification/Sensitivity_Mom2Param');
% close(Fig);